import request from '@/utils/request'
import qs from "qs";

export function downLoad(url, param, fileName) {
  const prams = qs.stringify(param, { indices: false })
  if (param) {
    url = `${url}?${prams}`
  }
  request.get(url, {responseType: 'blob'}).then(res => {
    var blob = new Blob([res],
      {
        type: 'application/vnd.ms-excel'
      });
    var downloadElement = document.createElement('a');
    var href = window.URL.createObjectURL(blob); //创建下载的链接
    downloadElement.style.display = 'none';
    downloadElement.href = href;
    downloadElement.download = fileName;
    document.body.appendChild(downloadElement);
    downloadElement.click(); //点击下载
    document.body.removeChild(downloadElement); //下载完成移除元素
    window.URL.revokeObjectURL(href); //释放掉blob对象
  })
}

export function downLoadFile(url, param) {
  let href = url
  if (param) {
    const prams = qs.stringify(param, { indices: false })
    href = `${url}?${prams}`
  }

  var downloadElement = document.createElement('a');
  downloadElement.style.display = 'none';
  downloadElement.href = href;
  downloadElement.click(); //点击下载
  document.body.removeChild(downloadElement); //下载完成移除元素
}
